<script type="text/javascript">
	function doSearchMaterial(value) {
		
	}
	
	//表格查询  
	function reloadMaterial() {
		var params = material_list.datagrid('options').queryParams; //先取得 datagrid 的查询参数  
		printLog(params);
		material_list.datagrid('reload'); //设置好查询参数 reload 一下就可以了  
		material_list.datagrid('unselectAll');
		material_list.datagrid('uncheckAll');
	}


	function addMaterial() {
		createFromWindow("新增信息", "material.do?editMaterial", "material.do?saveMaterial",
				function(data) {
					reloadMaterial();
				});
	}
	
	function deleteMaterial() {
		var rows = material_list.datagrid('getChecked');
	    if (rows&&rows.length>0){
	        $.messager.confirm('询问','你确认要删除吗?',function(r){
	            if (r){
	            	var ids = "";
	            	for(var i=0; i<rows.length; i++){
	            		ids += rows[i].id+",";
	            	}
	                $.post('material.do?deleteMaterialItems',{ids:ids},function(result){
	                    if (result.success){
	                    	reloadMaterial();    
	                    }
	                    showMsg(result.msg);
	                },'json');
	            }
	        });
	    }else {
	    	showMsg("请选择操作记录.");
	    }
	}

	function editMaterial() {
		var rows = material_list.datagrid('getChecked');
		printLog(rows);
		if (rows&&rows.length>0) {
			if(rows.length>1) {
				showMsg('请选择一条操作记录.');
				return;
			}
			createFromWindow("编辑信息", "material.do?editMaterial&materialId=" + rows[0].id,
					"material.do?saveMaterial", function(data) {
						reloadMaterial();
					},true);
		} else {
			showMsg('请选择操作记录.');
		}

	}
	
	
	//操作按钮触发
	function operationsLookMaterial(id) {
		if (id == undefined) {//点击右键菜单才会触发这个
			var rows = material_list.datagrid('getSelections');
			id = rows[0].id;
		}
		if (id) {
			createFromWindow("查看信息", "material.do?editMaterial&materialId=" + id,
					null, null,false);
		} else {
			showMsg('请选择查看记录.');
		}
	}
	
	function operationsEditMaterial(id) {
		if (id == undefined) {//点击右键菜单才会触发这个
			var rows = material_list.datagrid('getSelections');
			id = rows[0].id;
		}
		if (id) {
			createFromWindow("编辑信息", "material.do?editMaterial&materialId=" + id,
					"material.do?saveMaterial", function(data) {
						reloadMaterial();
					},true);
		} else {
			showMsg('请选择编辑记录.');
		}
	}
	
	function operationsSendMaterial() {
		printLog('发送');
	}
	
	function operationsDeleteMaterial(id) {
		if (id == undefined) {//点击右键菜单才会触发这个
			var rows = new_list.datagrid('getSelections');
			id = rows[0].id;
		}
		if (id) {
			$.messager.confirm('询问', '你确认要删除吗?', function(r) {
				if (r) {
					$.post('material.do?deleteMaterialItems', {
						ids : id
					}, function(result) {
						if (result.success) {
							reloadMaterial(); 
						}
						showMsg(result.msg);
					}, 'json');
				}
			});
		} else {
			showMsg("请选择操作记录.");
		}
	}
	
	function clearMaterialSearch() {
		setGridSearchParams('material_list','materialSearchForm',true,reloadMaterial);
	}
	
	//根据条件查询
	function materialSearch() {
		setGridSearchParams('material_list','materialSearchForm',false,reloadMaterial);
	}
	
	function exportMaterial() {
		var rows = material_list.datagrid('getChecked');
// 	    if (rows&&rows.length>0){
//            	var ids = "-1";
//            	for(var i=0; i<rows.length; i++){
//            		ids += ","+rows[i].id;
//            	}
        downLoadFile("material.do?exportMaterial&ids=1");
// 	    }else {
// 	    	showMsg("请选择操作记录.");
// 	    }
	}
	
	function importMaterial() {
// 		createFromWindow("导入文件", "material.do?importMaterial",
// 				"material.do?importMaterialInfo", function(data) {
// 					reloadMaterial();
// 				},true,400,200);
// 		createFromAutoSubmitWindow({
// 			title:"导入文件",
// 			maximizable:true,
// 			width:400,
// 			height:200,
// 			href:"material.do?importMaterial",
// 			submitUrl:"material.do?importMaterialInfo",
// 			callback:function(data) {
// 				reloadMaterial();
// 			},
// 			enableSaveButton:true
// 		});
		
		createFromWindowOptions({
			title:"导入文件",
			maximizable:false,
			width:400,
			height:200,
			href:"material.do?importMaterial",
			callback:function(data) {
				$('#fileForm').ajaxSubmit({
					async : false,
					dataType : 'json',
					success : function(data) 
					{
						showMsg(data.msg);
						reloadMaterial();
					},
					error : function(XMLHttpRequest, textStatus, errorThrown)
					{
						printLog('error');
						showMsg('请求处理错误');
					}
				});
			},
			enableSaveButton:true
		});
	}
</script>
<script type="text/javascript">
	function doSearchMaterialCategory(value) {
		
	}
	
	//表格查询  
	function reloadMaterialCategory() {
		var params = materialCategory_list.datagrid('options').queryParams; //先取得 datagrid 的查询参数  
		printLog(params);
		materialCategory_list.treegrid('reload'); //设置好查询参数 reload 一下就可以了  
		materialCategory_list.treegrid('unselectAll');
		materialCategory_list.treegrid('uncheckAll');
		clearMaterialSearch();
	}


	function addMaterialCategory() {
		var rows = materialCategory_list.datagrid('getChecked');
		printLog(rows);
		var mcParentId = -1;
 		if (rows&&rows.length>0) {
			if(rows.length!=1) {
				showMsg('至多只能选择一个父类别记录.');
				return;
			}else {
				mcParentId = rows[0].id;
			}
		}
		createFromWindow("新增信息", "materialCategory.do?editMaterialCategory&mcParentId="+ mcParentId, "materialCategory.do?saveMaterialCategory",
				function(data) {
					reloadMaterialCategory();
				});
	}
	
	function deleteMaterialCategory() {
		var rows = materialCategory_list.datagrid('getChecked');
	    if (rows&&rows.length>0){
	        $.messager.confirm('询问','你确认要删除吗?',function(r){
	            if (r){
	            	var ids = "";
	            	for(var i=0; i<rows.length; i++){
	            		ids += rows[i].id+",";
	            	}
	                $.post('materialCategory.do?deleteMaterialCategoryItems',{ids:ids},function(result){
	                    if (result.success){
	                    	reloadMaterialCategory();    
	                    }
	                    showMsg(result.msg);
	                },'json');
	            }
	        });
	    }else {
	    	showMsg("请选择操作记录.");
	    }
	}

	function editMaterialCategory() {
		var rows = materialCategory_list.datagrid('getChecked');
		printLog(rows);
		if (rows&&rows.length>0) {
			if(rows.length>1) {
				showMsg('请选择一条操作记录.');
				return;
			}
			createFromWindow("编辑信息", "materialCategory.do?editMaterialCategory&materialCategoryId=" + rows[0].id,
					"materialCategory.do?saveMaterialCategory", function(data) {
						reloadMaterialCategory();
					},true);
		} else {
			showMsg('请选择操作记录.');
		}

	}
	
	
	//操作按钮触发
	function operationsLookMaterialCategory(id) {
		if (id == undefined) {//点击右键菜单才会触发这个
			var rows = materialCategory_list.datagrid('getSelections');
			id = rows[0].id;
		}
		if (id) {
			createFromWindow("查看信息", "materialCategory.do?editMaterialCategory&materialCategoryId=" + id,
					null, null,false);
		} else {
			showMsg('请选择查看记录.');
		}
	}
	
	function operationsEditMaterialCategory(id) {
		if (id == undefined) {//点击右键菜单才会触发这个
			var rows = materialCategory_list.datagrid('getSelections');
			id = rows[0].id;
		}
		if (id) {
			createFromWindow("编辑信息", "materialCategory.do?editMaterialCategory&materialCategoryId=" + id,
					"materialCategory.do?saveMaterialCategory", function(data) {
						reloadMaterialCategory();
					},true);
		} else {
			showMsg('请选择编辑记录.');
		}
	}
	
	function operationsSendMaterialCategory() {
		printLog('发送');
	}
	
	function operationsDeleteMaterialCategory(id) {
		if (id == undefined) {//点击右键菜单才会触发这个
			var rows = new_list.datagrid('getSelections');
			id = rows[0].id;
		}
		if (id) {
			$.messager.confirm('询问', '你删除该类别后，对应其下的子类别和耗材列表会同时删除！你确认要删除吗?', function(r) {
				if (r) {
					$.post('materialCategory.do?deleteMaterialCategoryItems', {
						ids : id
					}, function(result) {
						if (result.success) {
							reloadMaterialCategory(); 
						}
						showMsg(result.msg);
					}, 'json');
				}
			});
		} else {
			showMsg("请选择操作记录.");
		}
	}
	
	function onDblClickMaterialRow(rowIndex, rowData) {
		createFromWindow("编辑信息", "material.do?editMaterial&materialId=" + rowData.id,
				"material.do?saveMaterial", function(data) {
					reloadMaterial();
				},true);
	}
</script>

<div class="easyui-layout" data-options="fit:true">
	<div data-options="region:'center',border:false">
		
		<@eu.datagrid id="material_list" toolbar="#materiallist_search" idField="id"
			 url="material.do?loadList" rownumbers="true" onDblClickRow="onDblClickMaterialRow(rowIndex, rowData)"
			 item="com.xdtech.ad.vo.MaterialItem"/>
		
		<div id="materiallist_search">
			<div style="margin-bottom:5px;padding-left:5px;margin-top: 5px;">
				<form id="materialSearchForm">
					<!-- 防止form表单里只有一个text元素时候，按enter键自动提交问题 -->
					<input style="display:none" />
					<table>
						<tr>
							<td>名称：</td>
							<td><input class="easyui-validatebox textbox" name="name" /></td>
							<td>
								<@eu.linkbutton showName="查询" iconCls="icon-search" onclick="materialSearch()"/>
	 					 		<@eu.linkbutton showName="清空" iconCls="icon-clear" onclick="clearMaterialSearch()"/>
 					 		</td>
						</tr>
					</table>
				</form>
				
			</div>
			<div id="material_toolbar" style="height: auto">
				<@eu.linkbutton showName="新增" shiro="ad:material:add" iconCls="icon-add" onclick="addMaterial()"/>
				<@eu.linkbutton showName="删除" shiro="ad:material:delete" iconCls="icon-remove" onclick="deleteMaterial()"/>
				<@eu.linkbutton showName="编辑" shiro="ad:material:edit" iconCls="icon-edit" onclick="editMaterial()"/>
				<@eu.linkbutton showName="导入" shiro="ad:material:import" iconCls="icon-import" onclick="importMaterial()"/>
				<@eu.linkbutton showName="导出" shiro="ad:material:export" iconCls="icon-export" onclick="exportMaterial()"/>
				<@eu.linkbutton showName="刷新" shiro="ad:material:refresh" iconCls="icon-reload" onclick="reloadMaterial()"/>
				
			</div>
		</div>
	</div>
</div>